package server;

import java.io.*;

/**
 * @author Will
 *	the TXN class record the transaction information
 * This is the test of SVN
 */
public class TXN implements Serializable{
	private static final long serialVersionUID = 794836013779061074L;	
	  int TXNID;
	  String command;
	  String filePath;
	  String fileName;	
	  
	  int totalNumber;  
	  String receivedSequence;	  
	  String dataToDisk;
	  
	 /**
	 * @param id - the transaction id
	 * @param fileName - the transaction filename
	 */
//	public static void main (String args[]) {
//		  TXN txn = new TXN(0,"test","./","file");			  
//		  try {
//			txn.writeLog();
//			txn.commit();
//			txn.readLog();
//			  
//			} catch (IOException e) {
//			e.printStackTrace();
//			return ;
//			}
//		  
//	   } 
	  
	TXN(int id,String command,String filePath,String fileName){
		TXNID=id;
		totalNumber=0;
		receivedSequence="";
		this.filePath=filePath;
		this.fileName=fileName;
		this.command=command;
		dataToDisk="";
	  }
	
	@Override
	public String toString(){
		return new StringBuffer("TXNID: ").append(this.TXNID)
		.append("; command: ").append(this.command)
		.append("; filePath: ").append(this.filePath)
		.append("; fileName: ").append(this.fileName)
		.append("; totalNumber: ").append(this.totalNumber)
		.append("; receivedSequence: ").append(this.receivedSequence)	
		.append("; dataToDisk: ").append(this.dataToDisk)
		.toString();  
	  }	
	
	public void writeLog() throws IOException {
		// to store the log in file system, file name is TXNID, using "." to hide the log
		File toFile=new File(filePath,"."+(new Integer(TXNID).toString()));
		if(toFile.canRead()!=true){
			//cannot open the file, so create a new file and set the createdAFile tag
			toFile.createNewFile();	
		}		
		FileOutputStream fout= new FileOutputStream(toFile);		
		ObjectOutputStream oos=new ObjectOutputStream(fout);
		oos.writeObject(this);
		System.out.println("Writing");
		System.out.println(this);
		oos.close();	
		return ;	
	}
	
	public TXN readLog(){
		TXN txn;
		try{
			File toFile=new File(filePath,"."+(new Integer(TXNID).toString()));
			FileInputStream fin= new FileInputStream(toFile);		
			ObjectInputStream input=new ObjectInputStream(fin);
			txn=(TXN)input.readObject();
			System.out.println("reading:");
			System.out.println(txn);
		   return txn;
 
	   }catch(Exception ex){
		   ex.printStackTrace();
		   System.out.println("Cannot reading log");
		   return null;
		   } 
	}
	
	public void commit() throws IOException{				
		File toFile= new File(filePath,fileName);		
		if(toFile.canRead()!=true){
		//cannot open the file, so create a new file and set the createdAFile tag
			toFile.createNewFile();
		}
		// flush to file system
		FileWriter fw = new FileWriter(toFile);
		BufferedWriter bw=new BufferedWriter(fw);
		bw.write(dataToDisk);
		bw.flush();	
		return ;
	}//end flush()
	
	public void addData(String append){
		dataToDisk=dataToDisk+append+"\n";
	}
	
	public void addSequence(String append){
		receivedSequence=receivedSequence+append+" ";
	}
	
	public String findResendSeq(int totalSeq){
		String totalSeqString="";
		for(int i=0;i<totalSeq;++i){
			totalSeqString=totalSeqString+(new Integer(i).toString())+" ";	
		}
		String[] temp=receivedSequence.split("\\s+");
		String[] temp2=totalSeqString.split("\\s+");
		String content="";
		if(temp2.length>temp.length){
			for(int i=0;i<temp2.length;i++){
				int k=0;
				for(int j=0;j<temp.length;j++){
				 if(!temp[j].equals(temp2[i])){
					k++;
				 }
				 if(k==temp.length){
					 content=content+temp2[i]+" ";
				 }
				}
			}
		}
		return content;
		
	}
}
